package com.rdc.example.controller;

import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.FollowDTO;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.service.impl.FollowServiceImpl;
import com.rdc.example.view.UserView;

import java.util.ArrayList;
import java.util.List;

import static com.rdc.example.controller.UserController.utilView;

/**
 * @author duck
 */
public class FollowController {
    private static final FollowServiceImpl followService=new FollowServiceImpl();
    private List<FollowDTO> followDTOList=new ArrayList<>();

    public static void main(String[] args) {
        FollowController followController=new FollowController();
        turnFollowerId(1,2);
    }

    public  void insertFollow(Integer followerId, Integer followedId){
        if (followerId==null){
            utilView.showError("followerId is null");
            return;
        }
        if (followedId==null){
            utilView.showError("followedId is null");
        }

        try{
            FollowDTO followDTO = new FollowDTO();
            followDTO.setFollowerId(followerId);
            followDTO.setFollowedId(followedId);
            Result result = followService.insertFollow(followDTO);
            utilView.showMessage(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void deleteFollowByFollowerIdFollowedId(Integer followerId, Integer followedId){
        if (followerId==null){
            utilView.showError("followerId is null");
            return;
        }
        if (followedId==null){
            utilView.showError("followedId is null");
        }

        try{
            Result result = followService.deleteFollowByFollowerIdFollowedId(followerId,followedId);
            utilView.showMessage(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void turnFollowerId(Integer followerId, Integer followedId){
        if (followerId==null){
            utilView.showError("followerId is null");
            return;
        }
        if (followedId==null){
            utilView.showError("followedId is null");
            return;
        }
        try{
            FollowDTO followDTO = new FollowDTO();
            followDTO.setFollowerId(followerId);
            followDTO.setFollowedId(followedId);
            Result result = followService.turnFollow(followDTO);
            utilView.showMessage(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    public static void deleteFollowById(Integer followId){
        if (followId==null){
            utilView.showError("followerId is null");
            return;
        }
        try{
            Result result = followService.deleteFollowById(followId);
            utilView.showMessage(result.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void showListFollowByUserId(Integer userId){
        if (userId==null){
            utilView.showError("Id is null");
            return;
        }
        try{
            List<UserDTO> userDTOList = followService.listUserDTOByFollowerId(userId);
            if (userDTOList==null|| userDTOList.isEmpty()){
                utilView.showMessage("关注列表为空");
                return;
            }
            UserView userView=new UserView();
            for(int i=0;i<userDTOList.size();i++){
                userView.showUserDTOList(i,userDTOList.get(i));
            }
            MainController.currentUserDTOList = userDTOList;
        }catch (Exception e){
            e.printStackTrace();
        }
    }











}
